import { createStore } from 'vuex'
import api from '@/api/baseApi';
// import createPersistedState from 'vuex-persistedstate';
export default createStore({
  state: {
    isLogin: null,
    info: null,
    storeList: null
  },
  getters: {
  },
  mutations: {
    changeIsLogin(state, flag){
      state.isLogin = flag
    },
    changeInfo(state, payload) {
      state.info = payload;
    },
    changeStoreList(state, payload){
      state.storeList = payload;
    },
    removeStoreList(state, id) {
      if (state.storeList === null) return;
      state.storeList = state.storeList.filter(item => {
          return +item.id !== +id;
      });
    }
  },
  actions: {
    async changeIsLoginAsync({ commit }){
      let flag = false
      const { code } = await api.checkLogin()
      if(+code === 0){
        flag = true
      }
      commit('changeIsLogin', flag)
    },
    async changeInfoAsync({ commit }) {
      let { code, data } = await api.userInfo();
      if (+code === 0) {
          commit('changeInfo', data);
      }
    },
    async changeStoreListAsync({ commit }) {
        let { code, data } = await api.storeList();
        if (+code !== 0) data = [];
        commit('changeStoreList', data);
    }
  },
  modules: {
  },
  // plugins: [createPersistedState()]
})
